/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd
 ******************************************************************************
 *  @File Name        : kf8a100fxx_flash.h
 *  @Author           : ChipON AE/FAE Group
 *  @Date             : 2023-12-19
 *  @HW Version       : KF8A100Fxx
 *  @Lib Version      : V2.0.1
 *  @Description      : This file provides the driver for the flash module
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd
 *  All rights reserved.
 *
 *  This software is copyright protected and proprietary to
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.
 *****************************************************************************/
/******************************************************************************
 *  |Date        |Version  |Author       |Description
 ******************************************************************************
 *  |2023-12-25  |V2.0     |Zhong Yusheng|New creat
 *****************************************************************************/

#ifndef KF8A100FXX_FLASH_H
#define KF8A100FXX_FLASH_H

#ifdef __cplusplus
extern "C" {
#endif

/******************************************************************************
 *                      Include Files
 *****************************************************************************/
#include "kf8a100fxx.h"
/******************************************************************************
 *                      Macro or Inline
 *****************************************************************************/
#define FLASH_BUFFER_WORD_SIZE  32u     // half page data length
#define FLASH_EFFECTIVE_ADDRESS 0x20u   // Address length of a half page of flash
#define FLASH_ADDRESS_START     0x0800u // Self erasing start address
#define FLASH_ADDRESS_END       0x3FC0u // Self erasing end address
/*****************************************************************************
 *                      Typedef Definitions
 *****************************************************************************/
typedef enum
{
    FLASH_CMD_SUCCESS = 0x00u,
    FLASH_CMD_ERROR   = 0xAAu,
    FLASH_PARAM_ERROR = 0x0Cu
} Flash_CmdType;
/******************************************************************************
 *                      Export Variables
 *****************************************************************************/

/* Flash self-write key: Flash_Key_ONE = 0x69, Flash_Key_TWO = 0x96 */
extern volatile uint8_t Flash_Key_ONE, Flash_Key_TWO;

/******************************************************************************
 *                      Export Functions
 *****************************************************************************/

/* Note: Flash self-read and write related functions are only available on Fxx series chips version A01 and later. */

/**
 * @brief Data flash reads out a double byte of data at a specified address
 *
 * @param Addr flash addr: FLASH_ADDRESS_START~FLASH_ADDRESS_END and is a multiple of FLASH_EFFECTIVE_ADDRESS.
 * @return uint16_t flash data
 */
uint16_t FLASH_READ_ONE(uint16_t Addr);

/**
 * @brief Write data to FLASH by half page
 *
 * @attention 1. This function cannot be called within an interrupt service routine.
 * 2. Write the KEY before calling the function, and clear the KEY after the function call.
 * 3. When writing to Flash, starting from the self-write address 800H of the Flash, 16 consecutive words form a data
 * block, and 2 consecutive data blocks form a page.
 * 4. When writing to Flash, regardless of whether the storage unit contains data, an erase operation must be performed
 * first. The erase operation only takes place when writing the first data block of each page, erasing all unit data on
 * that page. No erase operation occurs when writing other data blocks on the page.
 * 5. After writing the first data block of each page, the CPU will pause for 6ms to perform the erase and write
 * operations. When writing other blocks, the CPU will pause for 3ms to perform the write operation.
 *
 * @param Addr Flash addr: FLASH_ADDRESS_START~FLASH_ADDRESS_END and is a multiple of FLASH_EFFECTIVE_ADDRESS.
 * @param Buff Data pointer
 * @return Write falsh status
 */
Flash_CmdType Flash_Write_HalfPage(uint16_t Addr, const uint16_t *Buff);

/**
 * @brief Read half page of data from FLASH
 *
 * @attention This function cannot be called within an interrupt service routine.
 *
 * @param Addr Flash addr
 * @param Buff Data pointer
 */
void Flash_Read_HalfPage(uint16_t Addr, uint16_t *Buff);
#ifdef __cplusplus
}
#endif

#endif
/* EOF */
